var grid;
var editCheck = $("#editCheck").val(); // 表格权限按钮控制
var dtGridColumns = [
		{
			id : 'id',
			title : 'ID',
			type : 'string',
			columnClass : 'text-center',
			headerStyle : 'color:grey;'
		},
		{
			id : 'agentName',
			title : '合伙人姓名',
			type : 'string',
			columnClass : 'text-center',
			headerStyle : 'color:grey;'
		},
		{
			id : 'mobile',
			title : '合伙人手机号',
			type : 'string',
			columnClass : 'text-center',
			headerStyle : 'color:grey;'
		},
		{
			id : 'gradeDesc',
			title : '合伙人等级',
			type : 'string',
			columnClass : 'text-center',
			headerStyle : 'color:grey;'
		},
		{
			id : 'agentArea',
			title : '管辖区域',
			type : 'string',
			columnClass : 'text-center',
			headerStyle : 'color:grey;'
		},
		{
			id : 'upLevelGradeDesc',
			title : '上级合伙人等级',
			type : 'string',
			columnClass : 'text-center',
			headerStyle : 'color:grey;'
		},
		{
			id : 'upLevelAgentName',
			title : '上级合伙人姓名',
			type : 'string',
			columnClass : 'text-center',
			headerStyle : 'color:grey;'
		},
		{
			id : 'upLevelMobile',
			title : '上级合伙人手机号',
			type : 'string',
			columnClass : 'text-center',
			headerStyle : 'color:grey;'
		},
		{
			id : 'isExitCredit',
			title : '当前授信总额（元）',
			type : 'string',
			columnClass : 'text-center',
			headerStyle : 'color:grey;'
		},
		{
			id : 'creditLimit',
			title : '授信额上限（万）',
			type : 'string',
			columnClass : 'text-center',
			headerStyle : 'color:grey;'
		},
		{
			id : 'creditSwitch',
			title : '授信权限状态',
			type : 'string',
			columnClass : 'text-center',
			headerStyle : 'color:grey;'
		},
		{
			id : 'operation',
			title : '操作',
			type : 'string',
			columnClass : 'text-center tpl-table-black-operation',
			headerStyle : 'color:grey;',
			resolution : function(value, record, column, grid, dataNo, columnNo) {
				var content = '';
				if(editCheck){
					content += '<a href="#" onclick="editSxLimit('+record.id+','+record.creditLimit+');" ><i class="am-icon-pencil"></i> 编辑</a>';
				}
				return content;
			}
		} ];

var dtGridOption = {
	lang : 'zh-cn',
	ajaxLoad : true,
	check : true,
	checkWidth : '37px',
	loadURL : 'agentManage/sxPermissionList',
	columns : dtGridColumns,
	gridContainer : 'dtGridContainer',
	toolbarContainer : 'dtGridToolBarContainer',
	tools : '',
	pageSize : 10,
	pageSizeLimit : [ 10, 20, 30 ]
};

$(function() {
	grid = $.fn.dlshouwen.grid.init(dtGridOption);
	grid.load();
	$("#grade").val("10");
	$("#grade").attr("disabled", true);
});

function search() {
	grid.pager.startRecord = 0;
	grid.pager.nowPage = 1;
	grid.parameters = new Object();
	grid.parameters['agentkey'] = $("#agentkey").val();
	grid.parameters['grade'] = $("#grade").val();
	grid.parameters['creditSwitch'] = $("#creditSwitch").val();
	grid.parameters['agentArea'] = $("#agentArea").val();
	grid.refresh(true);
}

function editSxLimit(agentId,creditLimit){
	$('#agentIdHidden').val(agentId);
	$('#creditLimit').val(creditLimit);
	$('#my-prompt').modal({
	      relatedTarget: this,
	      onConfirm: function(e) {
	    	  saveSxLimit("agentManage/saveSxLimit",search,$('#agentIdHidden').val(),e.data);
	      },
	      onCancel: function(e) {
	        
	      }
	 });
}

function permissionOn() {
	var rows = grid.getCheckedRecords();
	var length = rows.length;
	if (length == 0) {
		layer.msg("你没有选择行", {
			icon : 0
		});
	} else {
		var ids = [];
	    $.each(rows, function(index,val) {
	    	ids[index] = val.id;
	    });
	    var idsString = ids.join(",");
		able("agentManage/permissionOn",search,idsString);
	}
}

function permissionOff() {
	var rows = grid.getCheckedRecords();
	var length = rows.length;
	if (length == 0) {
		layer.msg("你没有选择行", {
			icon : 0
		});
	} else {
		var ids = [];
	    $.each(rows, function(index,val) {
	    	ids[index] = val.id;
	    });
	    var idsString = ids.join(",");
	    disable("agentManage/permissionOff",search,idsString);
	}
}

function able(nav,callback,ids){
	layer.confirm('确认启用吗？', {
        icon : 3,
        title : '启用选中代理人授信权限'
    }, function(index, layero) {
        $.ajax({
            type : "POST",
            url :  nav,
            dataType : "json",
            data: { ids : ids },
            success : function(resultdata) {
                if (resultdata.ret=="200") {
                	if(resultdata.content.code != 10000){
                		layer.msg(resultdata.content.msg, {
                            icon : 0
                        });	
                	}else{
                		layer.msg(resultdata.message, {
                            icon : 1
                        });
                	}
                    if (callback) {
                        callback();
                    }
                } else {
                    layer.msg(resultdata.message, {
                        icon : 0
                    });
                }
            },
            error : function(errorMsg) {
                layer.msg('服务器未响应,请稍后再试', {
                    icon : 3
                });
            }
        });
        layer.close(index);
    });
}

function disable(nav,callback,ids){
	layer.confirm('确认禁用吗？', {
        icon : 3,
        title : '禁用选中代理人授信权限'
    }, function(index, layero) {
        $.ajax({
            type : "POST",
            url :  nav,
            dataType : "json",
            data: { ids : ids },
            success : function(resultdata) {
                if (resultdata.ret=="200") {
                    layer.msg(resultdata.message, {
                        icon : 1
                    });
                    if (callback) {
                        callback();
                    }
                } else {
                    layer.msg(resultdata.message, {
                        icon : 0
                    });
                }
            },
            error : function(errorMsg) {
                layer.msg('服务器未响应,请稍后再试', {
                    icon : 3
                });
            }
        });
        layer.close(index);
    });
}

function saveSxLimit(nav,callback,id,creditLimit){
	
    $.ajax({
        type : "POST",
        url :  nav,
        dataType : "json",
        data: { "id" : id ,"creditLimit" : creditLimit},
        success : function(resultdata) {
            if (resultdata.ret=="200") {
                layer.msg(resultdata.message, {
                    icon : 1
                });
                if (callback) {
                    callback();
                }
            } else {
                layer.msg(resultdata.message, {
                    icon : 0
                });
            }
        },
        error : function(errorMsg) {
            layer.msg('服务器未响应,请稍后再试', {
                icon : 3
            });
        }
    });
}
